package com.hrsystem.controller;

import com.hrsystem.common.Res;
import com.hrsystem.entity.News;
import com.hrsystem.entity.PersonalData;
import com.hrsystem.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/news")
public class NewsController {

    @Autowired
    NewsService newsService;

    /**
     * 根据管理员id查出所有的资讯
     * @param adminId
     * @return
     */
    @GetMapping("/checkall")
    public Res getListAll(Integer adminId) {
        List<News> news = newsService.getListAll(adminId);
        if (news != null) {
            return Res.create(1, "资讯查询成功", news);
        }
        return Res.create(0, "查询失败", null);
    }

    @PostMapping("/intonews")
    public Res getIntoNews(@RequestBody News news) {
        News getIntoNews = newsService.getIntoNews(news);
        System.out.println(news);
        if(getIntoNews.getAdminId() < 0){
            return Res.create(0, "添加资讯失败！你不是管理员！", null);
        } else if (getIntoNews == null && getIntoNews.getTitle() == null) {
            return Res.create(0, "添加资讯失败！标题不能为空", null);
        } else {
            return Res.create(1, "添加资讯成功!", getIntoNews);
        }
    }

}
